package com.example.myapp.activities.add_bill;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import com.example.myapp.R;
import com.example.myapp.activities.main_activity.MainActivity;
import com.example.myapp.bill.Bill;
import com.example.myapp.database.BillDao;

import java.util.Calendar;
import java.util.Date;


public class AddBillActivity extends Activity implements View.OnClickListener {
    static final int DATE_DIALOG_ID = 1;

    BillDao billDao;

    private EditText nazwaET;
    private EditText dataET;
    private EditText kwotaET;

    private int mYear;
    private int mMonth;
    private int mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_bill);
        setTitle("Dodawanie nowego rachunku");

        billDao = new BillDao(this);

        nazwaET = (EditText) findViewById(R.id.nazwaETAddBill);
        dataET = (EditText) findViewById(R.id.dataETAddBill);
        kwotaET = (EditText) findViewById(R.id.kwotaETAddBill);

        dataET.setOnClickListener(this);

        Button anulujBtn = (Button) findViewById(R.id.anulujBtnAddBill);
        Button zapiszBtn = (Button) findViewById(R.id.zapiszBtnAddBill);

        anulujBtn.setOnClickListener(this);
        zapiszBtn.setOnClickListener(this);

        if (getIntent().getExtras() != null) {
            Date selectedDate = (Date) getIntent().getExtras().get("Data");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(selectedDate);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            int month = calendar.get(Calendar.MONTH) + 1;
            int year = calendar.get(Calendar.YEAR);

            dataET.setText(dayOfMonth + "." + month + "." + year);
        }

        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // display the current date (this method is below)
        updateDisplay();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dataETAddBill:
                showDialog(DATE_DIALOG_ID);
                break;
            case R.id.anulujBtnAddBill:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            case R.id.zapiszBtnAddBill:
                Bill bill = getBillFromFields();
                if (bill != null) {
                    billDao.save(bill);
                    Intent intent2 = new Intent(this, MainActivity.class);
                    startActivity(intent2);
                }

                break;
        }
    }

    // updates the date in the TextView
    private void updateDisplay() {
        dataET.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mDay).append("-")
                        .append(mMonth + 1).append("-")
                        .append(mYear));
    }

    private Bill getBillFromFields() {
        String nazwaRachunku = nazwaET.getText().toString();
        String data = dataET.getText().toString();
        String kwota = kwotaET.getText().toString();

        Bill bill = null;

        if (nazwaRachunku.trim().equals("") ||
                data.trim().equals("") ||
                kwota.trim().equals("")) {
            showNoNameDateOrAmountDialog();
        } else {
            bill = new Bill(nazwaRachunku, data, Float.parseFloat(kwota), false);
        }
        return bill;
    }

    private void showNoNameDateOrAmountDialog() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Informacja");
        alertDialog.setMessage("Nie podałeś wszystkich wymaganych danych.");
        alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertDialog.show();
    }

    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                        mDay);
        }
        return null;
    }
}
